package admin

import (
	"github.com/gin-gonic/gin"
	"github.com/shopspring/decimal"
	"mojieshi/define"
	"mojieshi/models"
	"net/http"
	"strconv"
	"time"
)

// 物流list
func FreightList(c *gin.Context) {
	var jsonData Newdata
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		define.Response.ResultFail(c, 10000, define.JsonData, "")
		return
	}
	var list []models.MFreight
	var count int64
	if jsonData.Page == 0 {
		jsonData.Page = -1
	}
	if jsonData.Limit == 0 {
		jsonData.Limit = -1
	}
	offsetVal := (jsonData.Page - 1) * jsonData.Limit
	if jsonData.Page == -1 && jsonData.Limit == -1 {
		offsetVal = -1
	}
	tx := models.DB.Model(new(models.MFreight)).Where("Aid=?", jsonData.Aid)
	if jsonData.Status > 0 {
		tx = tx.Where("status=?", jsonData.Status)
	}
	if jsonData.Name != "" {
		tx = tx.Where("name LIKE ?", "%"+jsonData.Name+"%")
	}
	if jsonData.Bid > 0 {
		tx = tx.Where("bid=?", 0).Or("bid=?", jsonData.Bid)
	}
	tx = tx.Count(&count).Limit(jsonData.Limit).Offset(offsetVal).Order("sort desc,id").Find(&list)
	if err != nil {
		define.Response.ResultFail(c, 10001, err.Error(), "")
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"data": map[string]interface{}{
			"list":  list,
			"count": count,
		},
	})
}

// 物流模版新增or修改
func FreightAdd(c *gin.Context) {
	//获取传值
	var jsonData models.MFreight
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  err,
		})
		return
	}
	if jsonData.ID > 0 {
		//修改数据
		var editData = map[string]interface{}{
			"pstype":      jsonData.Pstype,
			"name":        jsonData.Name,
			"type":        jsonData.Type,
			"pricedata":   jsonData.Pricedata,
			"status":      jsonData.Status,
			"sort":        jsonData.Sort,
			"free_price":  jsonData.FreePrice,
			"freeset":     jsonData.Freeset,
			"minpriceset": jsonData.Minpriceset,
			"minprice":    jsonData.Minprice,
			"minnumset":   jsonData.Minnumset,
			"minnum":      jsonData.Minnum,
			"formdata":    jsonData.Formdata,
			"sub_name":    jsonData.SubName,
		}
		err = models.DB.Model(new(models.MFreight)).Where("id=?", jsonData.ID).Updates(&editData).Error
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": 10003,
				"msg":  err.Error(),
			})
			return
		}
		//写入日志
		userId, _ := strconv.Atoi(c.GetHeader("userId")) //将字符串转为int
		userName, bid := models.UserName(userId)
		logData := "管理员：" + userName + "，修改了物流模版:" + strconv.Itoa(jsonData.ID) + "的内容"
		models.LogAdd(3, "修改物流模版", userId, logData, bid, jsonData.Aid)
	} else {
		var data models.MFreight
		data.Aid = jsonData.Aid
		data.Bid = jsonData.Bid
		data.Pstype = jsonData.Pstype
		data.Name = jsonData.Name
		data.Type = jsonData.Type
		data.Pricedata = jsonData.Pricedata
		data.SubName = jsonData.SubName
		data.Formdata = jsonData.Formdata
		data.Storetype = 0
		data.Status = jsonData.Status
		data.Sort = 0
		data.FreePrice = jsonData.FreePrice
		data.Freeset = jsonData.Freeset
		data.Pstimeset = 0
		data.Pstimedata = jsonData.Pstimedata
		data.Psprehour = 4
		data.Createtime = int(time.Now().Unix())
		data.PeisongJuli1 = 5.0
		data.PeisongFee1 = 3.00
		data.PeisongJuli2 = 1.0
		data.PeisongFee2 = 1.00
		data.PeisongLat = "29.868388"
		data.PeisongLng = "121.549792"
		data.PeisongRange = 2000
		data.Pscontenttype = 0
		data.Needlinkinfo = 0
		data.Minpriceset = 0
		data.Minprice = decimal.NewFromFloat(0.00)
		data.Minnumset = 0
		data.Minnum = 0
		data.Fwprice = decimal.NewFromFloat(0.00)
		data.PeisongRangetype = 0
		data.PeisongLng2 = "121.549792"
		data.PeisongLat2 = "29.868388"
		err = models.DB.Model(new(models.MFreight)).Create(&data).Error
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": 10001,
				"msg":  err.Error(),
			})
			return
		}
		//写入日志
		userId, _ := strconv.Atoi(c.GetHeader("userId")) //将字符串转为int
		userName, bid := models.UserName(userId)
		logData := "管理员：" + userName + "，新增了一个物流模版:" + jsonData.Name
		models.LogAdd(2, "新增物流模版", userId, logData, bid, jsonData.Aid)
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"msg":     "成功！",
	})
}

//删除物流模版

func FreightDel(c *gin.Context) {
	var jsonData models.MFreight
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  err,
		})
		return
	}
	var data models.MFreight
	models.DB.Model(new(models.MFreight)).Where("id=?", jsonData.ID).First(&data)
	tx := models.DB.Begin()

	//删除
	err = tx.Delete(&models.MFreight{}, jsonData.ID).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10004,
			"msg":  err.Error(),
		})
		tx.Rollback()
		return
	}
	tx.Commit()

	//写入日志
	userId, _ := strconv.Atoi(c.GetHeader("userId")) //将字符串转为int
	userName, bid := models.UserName(userId)
	logData := "管理员：" + userName + "，删除了一个物流模版:" + data.Name
	models.LogAdd(4, "删除物流模版", userId, logData, bid, jsonData.Aid)
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"msg":     "成功！",
	})
}

// 物流模版排序
func FreightSort(c *gin.Context) {
	//接收id
	var jsonData models.MFreight
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  err,
		})
		return
	}
	var data models.MFreight
	models.DB.Model(new(models.MFreight)).Where("id=?", jsonData.ID).First(&data)
	//修改
	err = models.DB.Model(new(models.MFreight)).Where("id=?", jsonData.ID).Update("sort", jsonData.Sort).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10002,
			"msg":  err.Error(),
		})
		return
	}
	//写入日志
	userId, _ := strconv.Atoi(c.GetHeader("userId")) //将字符串转为int
	userName, bid := models.UserName(userId)
	logData := "管理员：" + userName + "，修改物流模版:" + data.Name + "的排序"
	models.LogAdd(5, "物流模版", userId, logData, bid, jsonData.Aid)
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"msg":     "成功！",
	})
}
